package com.jerry.constellation.Utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class LoadDataDetialTask extends AsyncTask<String, Void, String> {

    Context context;
    OnGetNetDataListener listener;
    ProgressDialog dialog;
    boolean isshowDialog = false;

    private void initDialog() {
        dialog = new ProgressDialog(context);
        dialog.setTitle("提示信息：");
        dialog.setMessage("正在加载中......");
    }

    public LoadDataDetialTask(Context context, OnGetNetDataListener listener, boolean isshowDialog) {
        this.context = context;
        this.listener = listener;
        this.isshowDialog = isshowDialog;
        initDialog();
    }

    public interface OnGetNetDataListener {
        public void onSuccess(String json);
    }

    //运行在主线程中，通常在此方法中进行初始化
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (isshowDialog) {
            dialog.show();
        }

    }

    //运行在子线程中，可以在此处进行耗时操作（比如网络请求）
    @Override
    protected String doInBackground(String... params) {
        String json = HttpUtils.getJsonFromNet(params[0]);
        return json;
    }

    //运行在主线程中，可以在此处得到doInBackground返回的数据，通常进行控件的更新
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (isshowDialog) {
            dialog.dismiss();
        }
        listener.onSuccess(s);
    }
}
